package com.dongdong.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dongdong.mapper.TbBrandMapper;
import com.dongdong.model.TbBrand;
import com.dongdong.model.TbBrandExample;
import com.dongdong.page.PageParams;
import com.dongdong.po.BrandPO;
import com.dongdong.service.TbBrandService;
import com.dongdong.vo.BrandVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TbBrandServiceImpl implements TbBrandService {

    @Autowired
    private TbBrandMapper brandMapper;

    @Override
    public PageParams<TbBrand> getBranList(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbBrand> brandList = brandMapper.selectByExample(null);
        PageInfo<TbBrand> brandPageInfo = new PageInfo<>(brandList);
        PageParams<TbBrand> tbBrands = new PageParams(brandPageInfo.getPageNum(), brandPageInfo.getPageSize(),
                brandPageInfo.getList(), brandPageInfo.getTotal(), brandPageInfo.getPages());
        return tbBrands;
    }
//    @Override
//    public List<BrandVO> getBranList() {
//        List<TbBrand> brandList = brandMapper.selectByExample(null);
//        List<BrandVO> brandVOList = getBrandVOS(brandList);
//        return brandVOList;
//    }


    @Override
    public List<BrandVO> findBrandList(BrandVO brandVO) {
        TbBrandExample example = new TbBrandExample();
        if (!StrUtil.isEmpty(brandVO.getName())) {//名称不为空  模糊查询
            example.createCriteria().andNameLike("%" + brandVO.getName() + "%");
        }
        if (!StrUtil.isEmpty(brandVO.getFirstChar())) {//首字母不为空  精确查询
            example.createCriteria().andFirstCharEqualTo(brandVO.getFirstChar());
        }
        List<TbBrand> brandList = brandMapper.selectByExample(example);
        List<BrandVO> brandVOList = getBrandVOS(brandList);
        return brandVOList;
    }


    private List<BrandVO> getBrandVOS(List<TbBrand> brandList) {//由于两个方法相同,所以直接封装
        return brandList.stream().map((tbBrand) -> {//利用java8新特性将集合转为对象并且返回
            BrandVO brandVO = new BrandVO();
            BeanUtils.copyProperties(tbBrand, brandVO);
            return brandVO;
        }).collect(Collectors.toList());
    }

    //增加
    @Override
    public int insertBrand(BrandPO brandPO) {
        TbBrand tbBrand = new TbBrand();
        BeanUtils.copyProperties(brandPO, tbBrand);//将po中的数据复制到tbBrand中防止数据污染
        int insert = brandMapper.insert(tbBrand);
        return insert;
    }

    //回显
    @Override
    public BrandVO getBrandById(Long id) {
        BrandVO vo = new BrandVO();
        TbBrand tbBrand = brandMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(tbBrand, vo);
        return vo;
    }

    //删除
    @Override
    public int delteBrandAll(Long[] arr) {
        TbBrandExample example = new TbBrandExample();
        example.createCriteria().andIdIn(Arrays.asList(arr));
        int i = brandMapper.deleteByExample(example);
        return i;
    }

    //修改
    @Override
    public int updateBrandList(BrandPO brandPO) {
        TbBrand tbBrand = new TbBrand();
        BeanUtils.copyProperties(brandPO, tbBrand);
        int i = brandMapper.updateByPrimaryKeySelective(tbBrand);
        return i;
    }


    //获取select2
    @Override
    public List<BrandVO> getBrandListAndSelect2() {
        List<TbBrand> brandList = brandMapper.selectByExample(null);
        List<BrandVO> collect = brandList.stream().map(brandlist1 -> {
            BrandVO vo = new BrandVO();
            BeanUtils.copyProperties(brandlist1, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }
}
